package m.member.manage.action;

import m.common.action.ActionMeta;
import m.common.action.VuePageResult;
import m.common.action.VueResult;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.html.VueFormResult;
import m.html.VueListResult;
import m.html.element.attr.Align;
import m.html.element.attr.ButtonType;
import m.html.form.*;
import m.html.list.*;
import m.member.manage.model.ServeGoods;
import m.member.manage.model.ServeType;
import m.member.manage.service.ServeGoodsService;
import m.system.util.JSONMessage;
import m.system.util.ObjectUtil;
import m.system.util.StringUtil;
import manage.action.StatusAction;

import java.util.ArrayList;
import java.util.List;

@ActionMeta(name = "member/manage/serveGoods")
public class ServeGoodsAction extends StatusAction {
    private ServeGoods model;
    public JSONMessage doSave() throws Exception {
        verifyAdminOperPower(getStatusPower());
        String msg=getService(ServeGoodsService.class).save(model);
        return new JSONMessage(0)
                .push("msg",msg);
    }
    public VueResult toEdit() throws Exception {
        verifyAdminOperPower(getStatusPower());
        if(ObjectUtil.isEmptyOid(model)){
            model=new ServeGoods();
            model.setPrice(0d);
            model.setDiscount(0);
        }else{
            model= ModelQueryList.getModel(model,"*");
        }
        ModelForm form=new ModelForm("商品信息",3);
        form.addItem(
            FormInput.select("类型","model.type.oid","m.member.manage.model.ServeType").setPlaceholder("请选择类型"),
            FormInput.input("标题","model.title").setPlaceholder("请输入标题").setRequired(true).setSpan(2),
            FormInput.number("单价","model.price","元").setPlaceholder("请输入单价").setRequired(true),
            FormInput.number("优惠比例","model.discount","%").setPlaceholder("请输入优惠比例").setRequired(true),
            FormHtml.ins("优惠后","{{formatNumber(model.price*(100-model.discount)/100,\"#,##0.00\")}}元"),
            FormInput.textarea("描述","model.description").setPlaceholder("请输入描述").setSpan(3)
        )
        .addData("model", model)
        .addButton(
            FormButton.pri("保存","doSave")
        )
        .addMethod(
            FormEvent.backFlag,
            FormEvent.verify("verifyModel",
                FormJsTest.falseRemind("this.model.title","标题不能为空"),
                FormJsTest.falseRemind("this.model.price","单价不能为空"),
                FormJsTest.falseRemind("this.model.discount&&parseInt(this.model.discount)>=0&&parseInt(this.model.discount)<=100","优惠比例范围错误")
            ),
            FormEvent.doJson("doSave",actionUrl("doSave"),"{model:this.model}")
                .before("verifyModel").success("backFlag")
        );
        VueFormResult result=new VueFormResult();
        return result.addForm(form);
//        return new VuePageResult("page/member/manage/serveGoods.js")
//                .addParam("model", model);
    }
    /*
     * 服务商品管理
     */
    public VueResult allList() throws Exception {
        boolean power=hasAdminOperPower(getStatusPower());
        VueListResult result=getVueListResult("服务商品管理",ServeGoods.class,actionUrl("allList"));
        result.setSubTitle("服务商品创建后不要随意修改标题和描述，修改后会影响订单明细信息。");
        result.setFunction(true,false);
        result.setHeight(-1);
        result.addCol(toIndexColumn());
        result.addQueryFields("type.color","type.name");
        result.addCol(toTextColumn("类型", "type.oid", 100, true)
            .addElement(new ListElement("a-tag")
                .addAttr(":color","record.type.color")
                .addText("{{record.type.name}}")
            )
            .setDictType(ServeType.class)
            .setAlign(Align.CENTER)
        );
        result.addCol(toTextColumn("标题", "title", 200, true));
        result.addCol(toTextColumn("描述", "description", 300, true));
        result.addCol(toNumberColumn("单价","price",100,true).setCount(ListCount.NONE));
        result.addCol(toIntegerColumn("优惠","discount",100,true).setCount(ListCount.NONE)
            .addElement(ListElement.test("record.discount",
                new ListElement("a-tag")
                    .addAttr("color","processing")
                    .addText("{{record.discount}}%")
            )).setAlign(Align.CENTER)
        );
        result.addCol(toElementColumn("优惠后",
            new ListElement("span").addText("{{formatNumber(record.price*(100-record.discount)/100,'#,##0.00')}}")
        ).setAlign(Align.RIGHT));
        //状态列，第一个参数 是否可操作
        result.addCol(toStatusColumn(power, true));
        if(power) {
            result.addCol(toSpaceColumn("操作", 100,
                ListButton.modal("修改", ButtonType.PRIMARY, actionUrl("toEdit"), 800, SuccessMethod.REFRESH)
                    .addParam("model.oid", "oid")
            ).setFixed(Align.RIGHT));
            result.addButton(ListButton.modal("新增", ButtonType.PRIMARY, actionUrl("toEdit"), 800, SuccessMethod.REFRESH));
        }
        //排序 result.addOrder
        //查询条件 result.addCondition
        return result;
    }
    public VueResult selectList(){
        String selected=getParamsValue("selected");
        VueListResult result=getVueListResult("选择服务商品",ServeGoods.class,actionUrl("selectList"));
        result.setFunction(true,false);
        result.addCol(toIndexColumn());
        result.addQueryFields("oid","type.*");
        result.addCol(toTextColumn("类型", "type.oid", 100, true)
            .addElement(new ListElement("a-tag")
                .addAttr(":color","record.type.color")
                .addText("{{record.type.name}}")
            )
            .setDictType(ServeType.class)
            .setAlign(Align.CENTER)
        );
        result.addCol(toTextColumn("标题", "title", 200, true));
        result.addCol(toNumberColumn("单价","price",100,true).setCount(ListCount.NONE));
        result.addCol(toIntegerColumn("优惠","discount",100,true).setCount(ListCount.NONE)
            .addElement(ListElement.test("record.discount",
                new ListElement("a-tag")
                    .addAttr("color","processing")
                    .addText("{{record.discount}}%")
            )).setAlign(Align.CENTER)
        );
        result.addCol(toElementColumn("优惠后",
            new ListElement("span").addText("{{formatNumber(record.price*(100-record.discount)/100,'#,##0.00')}}")
        ).setAlign(Align.RIGHT));
        result.addCol(toTextColumn("描述", "description", 300, true));
        result.addCol(toSpaceColumn("操作", 80,
            ListElement.test("record.oid!='"+selected+"'",
                ListButton.back("选择", ButtonType.PRIMARY).addParam("", "")
            )
        ).setFixed(Align.RIGHT));
        String searchText=getParamsValue("searchText");
        if(StringUtil.notSpace(searchText)) {
            String[] arr=searchText.split("\\s");
            List<QueryCondition> cons=new ArrayList<>();
            cons.addAll(QueryCondition.manyLike("title",arr));
            cons.addAll(QueryCondition.manyLike("description",arr));
            result.addCondition(QueryCondition.or(cons.toArray(new QueryCondition[0])));
        }
        result.addSearch(ListSearch.custom("标题或描述","searchText",SearchType.STR_EQ).setSpan(2));
        return result;
    }

    @Override
    public String[] getStatusPower() {
        return new String[]{"app_serve_power"};
    }

    public ServeGoods getModel() {
        return model;
    }

    public void setModel(ServeGoods model) {
        this.model = model;
    }

}
